var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');


var app = express();

//所有路由前配置cors
const cors = require("cors")
app.use(cors())

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

//开放静态资源目录
app.use('/uploads', express.static("./uploads"))

//配置自己的路由
const index = require("./routes/index")
app.use("/index", index)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    res.send({
        success: false,
        status: 404,
        msg: "该请求不存在"
    })
});

// error handler
app.use(function(err, req, res, next) {
    res.send({
        success: false,
        status: 500,
        msg: err
    })
});

module.exports = app;